package com.qkbb.admin.kuibu.qkbb.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.PolylineOptions;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.qkbb.admin.kuibu.R;
import com.qkbb.admin.kuibu.qkbb.JavaBean.Point;
import com.qkbb.admin.kuibu.qkbb.funcation.SDCardHelper;
import com.qkbb.admin.kuibu.qkbb.url.Url;
import com.qkbb.admin.kuibu.qkbb.view.TitleBarView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class PlayBack extends AppCompatActivity {
    private MapView mapView;
    private AMap aMap;
    private ArrayList<Point> list;
    private int roadid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_back);
        Intent intent=getIntent();
        roadid=intent.getIntExtra("roadid",101);
        TitleBarView titleBarView= (TitleBarView) findViewById(R.id.foot_titlebar);
        titleBarView.setCenterTexiView("地图足迹");
        titleBarView.settextColor(Color.WHITE);
        titleBarView.setLeftButton(R.mipmap.fanhui_04);
        titleBarView.setLeftButtonOnclick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);// 必须要写
        aMap = mapView.getMap();

        // aMap.addPolyline(new PolylineOptions().add(latLng));
        getPoint(getApplication());
    }
    public List<Point> getPoint(Context context){

        String user_token= SDCardHelper.loadFileFromSdCard("user_token",getApplicationContext());
        list=new ArrayList<Point>();
        RequestQueue queue= Volley.newRequestQueue(context);
        StringRequest stringRequest=new StringRequest(Url.GETPOINT1 + user_token + Url.GETPOINT2 + roadid, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e("S", s);
                try {
                    JSONObject jsonObject =new JSONObject(s);
                    JSONObject jsonObject1=jsonObject.getJSONObject("data");
                    JSONArray jsonArray=jsonObject1.getJSONArray("Coords");
                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject2=jsonArray.getJSONObject(i);
                        Point point=new Point();
                        point.setDescription(jsonObject2.getString("description"));
                        point.setIdxno(jsonObject2.getInt("idxno"));
                        point.setLatitude(jsonObject2.getDouble("latitude"));
                        point.setLongitude(jsonObject2.getDouble("longitude"));
                        point.setRoadid(jsonObject2.getInt("roadid"));
                        point.setRoadlength(jsonObject2.getInt("roadlength"));
                        point.setRoadsteplength(jsonObject2.getInt("roadsteplength"));
                        point.setSteppercent(jsonObject2.getInt("roadsteplength"));
                        list.add(point);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                LatLng latLng[]=new LatLng[list.size()];
                for(int i=0;i<list.size();i++){
                    latLng[i]=new LatLng(list.get(i).getLatitude(),list.get(i).getLongitude());
                }
                aMap.addPolyline(new PolylineOptions().add(latLng).color(Color.YELLOW).width(10)).setVisible(true);
                aMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.mipmap.xiaolian)).position(latLng[0]).anchor(0.5f,0.5f));
                aMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.mipmap.zhongdian)).position(latLng[latLng.length-1]).anchor(0.5f,0.5f));
                CameraUpdate update= CameraUpdateFactory.newLatLngZoom(latLng[0], 14);
                aMap.moveCamera(update);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("volleyError", "" + volleyError);

            }
        });
        queue.add(stringRequest);
        return list;
    }
}
